/* eslint key-spacing:0 spaced-comment:0 */
const path = require('path')
const debug = require('debug')('app:config:project')
const argv = require('yargs').argv
const ip = require('ip')

debug('Creating default configuration.')
// ========================================================
// Default Configuration
// ========================================================
const config = {
  env : process.env.NODE_ENV || 'development',

  // ----------------------------------
  // Use docker
  // ----------------------------------
  docker: process.env.DOCKER || 'no',

  // ----------------------------------
  // Project Structure
  // ----------------------------------
  path_base  : path.resolve(__dirname, '..'),
  dir_client : 'src',
  dir_dist   : 'dist',
  dir_public : 'public',
  dir_server : 'server',
  dir_test   : 'tests',

  // ----------------------------------
  // Server Configuration
  // ----------------------------------

  server_host : ip.address(), // use string 'localhost' to prevent exposure on local network
  server_port : process.env.PORT || 3000,

  service_api: process.env.SERVICE_API || 'http://117.39.60.34:8770/v2/', // 测试接口
  websocket_api: process.env.WEBSOCKET_API || 'ws://10.1.10.15:8085/v1/', // 时序数据websocke请求接口
  kmax_api: process.env.KMX_API || 'http://117.39.60.34:28089/data-service/v4/', // 历史数据请求接口
  kmx_real_time_api: process.env.KMX_REAL_TIME_API || 'http://117.39.60.34:28082/data-service/v4/', // 实时数据接口
  specific_api: process.env.SPE_API || 'http://10.1.10.54:8090/', // 特征图谱接口
  kmx_tsdb_query_url: process.env.KMX_TSDB_QUERY_URL || 'http://117.39.60.34:8770/v2/device/',

  wx_service_api: process.env.WXSERVICE_API || 'https://www.k2data.net/sg/', // 测试接口
  wx_kmax_api: process.env.WXKMX_API || 'https://www.k2data.net/kmx/v4/data/', // 历史数据请求接口
  wx_kmx_real_time_api: process.env.WXKMX_REAL_TIME_API || 'https://www.k2data.net/kmx/v4/data/real/', // 实时数据接口
  wx_specific_api: process.env.WXSPE_API || 'https://www.k2data.net/drawtp/', // 特征图谱接口

  // ----------------------------------
  // Compiler Configuration
  // ----------------------------------
  compiler_babel : {
    babelrc        : false,
    cacheDirectory : true,
    plugins        : [
      'syntax-dynamic-import',
      'transform-runtime',
      'transform-flow-strip-types',
      ['import', [{ 'libraryName': 'antd', 'style': 'css' }]]
    ],
    presets        : [['latest', { es2015: { 'modules': false } }], 'stage-0', 'react'],
    env            : {
      development: {
        plugins: [ 'react-hot-loader/babel' ]
      }
    }
  },
  compiler_devtool         : 'eval-source-map',
  compiler_hash_type       : 'hash',
  compiler_fail_on_warning : false,
  compiler_quiet           : false,
  compiler_public_path     : '/',
  compiler_stats           : {
    chunks : false,
    chunkModules : false,
    colors : true
  },
  compiler_vendors : [
    'react',
    'react-redux',
    'react-router',
    'redux'
  ],

  // ----------------------------------
  // Test Configuration
  // ----------------------------------
  coverage_reporters : [
    { type : 'text-summary' },
    { type : 'lcov', dir : 'coverage' }
  ]
}

/************************************************
-------------------------------------------------

All Internal Configuration Below
Edit at Your Own Risk

-------------------------------------------------
************************************************/

// ------------------------------------
// Environment
// ------------------------------------
// N.B.: globals added here must _also_ be added to .eslintrc
config.globals = {
  'process.env'  : {
    'NODE_ENV' : JSON.stringify(config.env)
  },
  'NODE_ENV'     : config.env,
  '__DEV__'      : config.env === 'development',
  '__PROD__'     : config.env === 'production',
  '__TEST__'     : config.env === 'test',
  '__COVERAGE__' : !argv.watch && config.env === 'test',
  '__BASENAME__' : JSON.stringify(process.env.BASENAME || '')
}

if (config.docker !== 'yes') {
  config.globals['__SERVICE_API__'] = JSON.stringify(config.service_api)
  config.globals['__WEBSOCKET_API__'] = JSON.stringify(config.websocket_api)
  config.globals['__KMX_API__'] = JSON.stringify(config.kmax_api)
  config.globals['__KMX_REAL_TIME_API__'] = JSON.stringify(config.kmx_real_time_api)
  config.globals['__KMX_TSDB_QUERY_URL__'] = JSON.stringify(config.kmx_tsdb_query_url)
  config.globals['__SPE_API__'] = JSON.stringify(config.specific_api)

  config.globals['__WXSERVICE_API__'] = JSON.stringify(config.wx_service_api)
  config.globals['__WXKMX_API__'] = JSON.stringify(config.wx_kmax_api)
  config.globals['__WXKMX_REAL_TIME_API__'] = JSON.stringify(config.wx_kmx_real_time_api)
  config.globals['__WXSPE_API__'] = JSON.stringify(config.wx_specific_api)
}

// ------------------------------------
// Validate Vendor Dependencies
// ------------------------------------
const pkg = require('../package.json')

config.compiler_vendors = config.compiler_vendors
  .filter((dep) => {
    if (pkg.dependencies[dep]) return true

    debug(
      `Package "${dep}" was not found as an npm dependency in package.json; ` +
      `it won't be included in the webpack vendor bundle.
       Consider removing it from \`compiler_vendors\` in ~/config/index.js`
    )
  })

// ------------------------------------
// Utilities
// ------------------------------------
function base () {
  const args = [config.path_base].concat([].slice.call(arguments))
  return path.resolve.apply(path, args)
}

config.paths = {
  base   : base,
  client : base.bind(null, config.dir_client),
  public : base.bind(null, config.dir_public),
  dist   : base.bind(null, config.dir_dist)
}

// ========================================================
// Environment Configuration
// ========================================================
debug(`Looking for environment overrides for NODE_ENV "${config.env}".`)
const environments = require('./environments.config')
const overrides = environments[config.env]
if (overrides) {
  debug('Found overrides, applying to default configuration.')
  Object.assign(config, overrides(config))
} else {
  debug('No environment overrides found, defaults will be used.')
}

module.exports = config
